﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TakeoutExpress.Support.Controllers
{
    public class SiteController
    {
        private static Linq.TakeoutExpress2008DataContext context;

         static SiteController()
        {
            context = new TakeoutExpress.Support.Linq.TakeoutExpress2008DataContext();
        }
        public SiteController()
        {
        }

        public IEnumerable<Linq.Site> GetSubscribedSites(string userName)
        {
            var sites = (from s2 in context.Sites
                         join usersite in context.UserSites
                             on s2.SiteID equals usersite.SiteId
                         where usersite.Username == userName
                         
                         select s2);

            return sites;
        }

        public IEnumerable<Linq.Site> GetUnsubscribedSites(string userName)
        {

        
            var sites = (from s in context.Sites
                        select s).Except(from s2 in context.Sites
                                             join site in context.UserSites
                                                 on s2.SiteID equals site.SiteId
                                             where site.Username == userName
                                             select s2);

            return sites;
         
         
                        
        }

        public bool SubscribeToSite(string userName, int siteId)
        {
           
            TakeoutExpress.Support.Linq.UserSite userSite = new TakeoutExpress.Support.Linq.UserSite();

            userSite.SiteId = siteId;
            userSite.Username = userName;

            context.UserSites.InsertOnSubmit(userSite);
            context.SubmitChanges();

            return true;

        }

        public bool UnsubscribeFromSite(string username, int siteId)
        {
            var site = (from us in context.UserSites
                        where us.Username == username && us.SiteId == siteId
                        select us).Single();

            context.UserSites.DeleteOnSubmit(site);

            context.SubmitChanges();

            return true;
        }
        public IEnumerable<Linq.Site> GetAllSites()
        {
            var sites = (from s in context.Sites
                          select s);

            return sites;
        }

        public Linq.Site AddSite(string siteName)
        {
            Linq.Site site = new TakeoutExpress.Support.Linq.Site();
            site.SiteName = siteName;

            context.Sites.InsertOnSubmit(site);
           
            context.SubmitChanges();
          
            return site;
        }

        public Linq.Site GetSite(int siteID)
        {
            return (from s in context.Sites
                            where s.SiteID == siteID
                                select s).Single();
            
        }

        public Linq.Site GetDefaultSite()
        {
            return (from s in context.Sites
                    select s).FirstOrDefault();
        }
        public bool DeleteSite(int siteId)
        {
            var site = (from s in context.Sites
                        where s.SiteID == siteId
                        select s).Single();

            context.Sites.DeleteOnSubmit(site);

            context.SubmitChanges();
            return true;
        }

        public Linq.Site UpdateSite (Linq.Site site) {

            //Linq.TakeoutExpress2008DataContext db = new TakeoutExpress.Support.Linq.TakeoutExpress2008DataContext();
            Linq.Site updatedSite = GetSite(site.SiteID);//db.Sites.Single<Linq.Site>(o => o.SiteID == site.SiteID);

            //db.Sites.Attach(updatedSite, true);
            updatedSite.SiteName = site.SiteName;
            
            context.SubmitChanges();
          //  db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, db.Sites);

            return updatedSite;
        }

        public System.Collections.IEnumerable GetSiteAudiences(int siteId)
        {
            var siteAudiences = (from d in context.Audiences
                                 where d.SiteID == siteId
                                 select new
                                 {
                                    AudienceId = d.Id,
                                    AudienceName= d.Name,
                                    AudienceTotalMembers = d.AudienceMembers.Count,
                                    AudienceTotalTakeoutProviders = d.TakeoutProviderAudiences.Count
                                  
                                 });

            return (System.Collections.IEnumerable)siteAudiences.GetEnumerator();
        }
    }
}
